package com.xt.purchasingsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.purchasingsystem.dto.BuyerExecuteDto;
import com.xt.purchasingsystem.model.BuyerExecute;
import com.xt.purchasingsystem.service.BuyerExecuteService;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采购执行单所有接口
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@RestController
@RequestMapping("/buyerExecute")
public class BuyerExecuteController {
    @Autowired
    private BuyerExecuteService buyerExecuteService;

    /**
     *  添加采购执行单及其采购执行单明细的接口
     * @param buyerExecuteDto
     * @return
     */
    @PostMapping
    public Result saveBuyerExecute(@RequestBody BuyerExecuteDto buyerExecuteDto) {
           return buyerExecuteService.saveBuyerExecute(buyerExecuteDto);
    }

    /**
     * 添加采购执行单及其采购执行单明细修改采购计划明细执行单状态的接口
     * @param buyerExecuteDto
     * @return
     */
    @PostMapping("/saveBuyerExecuteAndUpdateBuyerPlanDetailExecuteTag")
    public Result saveBuyerExecuteAndUpdateBuyerPlanDetailExecuteTag(@RequestBody BuyerExecuteDto buyerExecuteDto){
     return  buyerExecuteService.saveBuyerExecuteAndUpdateBuyerPlanDetailExecuteTag(buyerExecuteDto);
    }

    /**
     * 分页查询未审核的采购执行单及其明细的接口
     * @param currNo
     * @param pageSize
     * @return
     */
    @GetMapping("/{currNo}/{pageSize}")
    public Result getAllBuyerExecuteNoCheck(@PathVariable Integer currNo,@PathVariable Integer pageSize){
        return buyerExecuteService.getAllBuyerExecuteNoCheck(currNo,pageSize);
    }

    @PutMapping
    public Result checkBuyerExecute(@RequestBody BuyerExecute buyerExecute){

        boolean flag = buyerExecuteService.updateById(buyerExecute);
        if (flag){
            return ResultFactory.buildSuccessResult("审核成功");
        }else {
            return ResultFactory.buildFailResult("审核失败");
        }
    }


    /**
     * 分页查询已审核未质检的采购执行单及其明细的接口
     * @param currNo
     * @param pageSize
     * @return
     */
    @GetMapping("getAllBuyerExecuteIsCheck/{currNo}/{pageSize}")
    public Result getAllBuyerExecuteIsCheck(@PathVariable Integer currNo,@PathVariable Integer pageSize){
        return buyerExecuteService.getAllBuyerExecuteIsCheck(currNo,pageSize);
    }

    /**
     * 查询所有已审核的执行单编号的接口
     * @return
     */
    @GetMapping("/getAllBuyerExecuteIdIsCheck")
    public Result getAllBuyerExecuteIdIsCheck(){
        QueryWrapper<BuyerExecute> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("ID").eq("CHECK_TAG","R001-1").eq("PUT_TAG","F001-0");
        List<BuyerExecute> list = buyerExecuteService.list(queryWrapper);
        List<String> buyerExecuteIds=new ArrayList<>();
        for (BuyerExecute buyerExecute : list) {
            buyerExecuteIds.add(buyerExecute.getId());
        }
        return ResultFactory.buildSuccessResult("查询成功",buyerExecuteIds);
    }


}

